package com.jxl.application.controller.interceptor;

import com.jxl.common.util.ContextUtil;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GlobleInterceptor implements HandlerInterceptor {

    /**
     * 请求进来的时候
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String loginId = request.getHeader("loginId");
        //储存到获取用户id的handler里面
        ContextUtil.setContext("loginId",loginId);
        return true;
    }


    /**
     * 请求完成的时候需要清空上下文数据
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        ContextUtil.removeContext();
    }
}
